package com.company.managesystem.activity;

import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;

import com.company.managesystem.R;
import com.company.managesystem.common.Apis;
import com.company.managesystem.common.DataFetch;
import com.company.managesystem.common.MyTextUtils;
import com.company.managesystem.model.UpdatPdState;
import com.company.managesystem.util.SharePreferenceUtil;

/**
 * 修改密码
 * Created by lqy on 2017/8/28.
 */

public class ChangePwdActivity extends BaseActivity {
    private static final String TAG = "ResetPwdActivity";
    private EditText oldPassword, newPassword, confirmPassword;
    private Button resetButton;
    private CheckBox oldCheck, newCheck, confirmCheck;
    private String oldPwd, newPwd, confirmPwd;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_changepwd);
    }

    @Override
    protected void initView() {
        setToobarBackIcon(R.mipmap.ic_arrow_back);
        setToolBarTitle("修改密码");
        oldPassword = (EditText) findViewById(R.id.old_password_edit);
        newPassword = (EditText) findViewById(R.id.new_password_edit);
        confirmPassword = (EditText) findViewById(R.id.new_confirm_password);
        resetButton = (Button) findViewById(R.id.reset_button);
        oldCheck = (CheckBox) findViewById(R.id.old_pwd_check);
        newCheck = (CheckBox) findViewById(R.id.new_pwd_check);
        confirmCheck = (CheckBox) findViewById(R.id.cb_pwd_check);
    }

    @Override
    protected void initData() {
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oldPwd = oldPassword.getText().toString().trim();
                newPwd = newPassword.getText().toString().trim();
                confirmPwd = confirmPassword.getText().toString().trim();
                if (MyTextUtils.isEmpty(oldPwd)) {
                    Toast.makeText(ChangePwdActivity.this, "请输入旧密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (MyTextUtils.isEmpty(newPwd)) {
                    Toast.makeText(ChangePwdActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (MyTextUtils.isEmpty(confirmPwd)) {
                    Toast.makeText(ChangePwdActivity.this, "请输入确认密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!confirmPwd.equals(newPwd)) {
                    Toast.makeText(ChangePwdActivity.this, "两次输入的密码不一致，请重新输入", Toast.LENGTH_SHORT).show();
                    return;
                }
                getData(oldPwd, newPwd);
            }
        });

        oldCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (oldCheck.isChecked()) {
                    oldPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    oldPassword.setSelection(oldPassword.getText().length());
                } else {
                    oldPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    oldPassword.setSelection(oldPassword.getText().length());
                }
            }
        });

        newCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (newCheck.isChecked()) {
                    newPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    newPassword.setSelection(newPassword.getText().length());
                } else {
                    newPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    newPassword.setSelection(newPassword.getText().length());
                }
            }
        });

        confirmCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (confirmCheck.isChecked()) {
                    confirmPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    confirmPassword.setSelection(confirmPassword.getText().length());
                } else {
                    confirmPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    confirmPassword.setSelection(confirmPassword.getText().length());
                }
            }
        });
    }

    private void getData(String oldPwd, String newPwd) {
        String userCode = new SharePreferenceUtil(this).getJsrCode();
        String url = Apis.api_changpwd + "?userCode=" + userCode + "&oldPass=" + oldPwd + "&newPass=" + newPwd;
        DataFetch dataFetch = new DataFetch<UpdatPdState>(this, url, false, UpdatPdState.class) {
            @Override
            protected void onDataLoaded(final UpdatPdState data) {
                super.onDataLoaded(data);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (data.getState() == 1) {
                            Toast.makeText(ChangePwdActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                            oldPassword.setText("");
                            newPassword.setText("");
                            confirmPassword.setText("");
                        } else {
                            Toast.makeText(ChangePwdActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            @Override
            protected void onDataLoadError() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ChangePwdActivity.this, "网络错误，请重新加载！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };
        dataFetch.LoadingData(false);
    }
}
